<?php

declare(strict_types=1);

namespace App\Model;

use Hyperf\DbConnection\Model\Model;

/**
 * @property int $id 
 * @property int $course_id 课程
 * @property int $section_id 章节
 * @property int $creator_id 
 * @property int $is_publish 
 * @property string $taskable_type 
 * @property int $taskable_id 
 * @property \Carbon\Carbon $created_at 
 * @property \Carbon\Carbon $updated_at 
 * @property-read null|\Hyperf\Database\Model\Model $taskable 
 * @property-read null|Course $course 
 * @property-read null|CourseSection $section 
 */
class CourseTask extends Model
{
    use Traits\HasCreator;
    /**
     * The table associated with the model.
     */
    protected ?string $table = 'course_task';

    /**
     * The attributes that are mass assignable.
     */
    protected array $fillable = ['id', 'video', 'data', 'course_id', 'section_id', 'creator_id', 'is_publish', 'taskable_type', 'taskable_id', 'created_at', 'updated_at'];

    /**
     * The attributes that should be cast to native types.
     */
    protected array $casts = ['id' => 'integer', 'video' => 'json', 'data' => 'json', 'course_id' => 'integer', 'section_id' => 'integer', 'creator_id' => 'integer', 'is_publish' => 'integer', 'taskable_id' => 'integer', 'created_at' => 'datetime', 'updated_at' => 'datetime'];


    public function taskable()
    {
        return $this->morphTo();
    }

    public function homeworks()
    {
        return $this->belongsToMany(Homework::class);
    }
    public function lives()
    {
        return $this->belongsToMany(Live::class, 'course_task_live_pivot');
    }
    public function course()
    {
        return $this->belongsTo(Course::class);
    }
    public function section()
    {
        return $this->belongsTo(CourseSection::class);
    }

    //用户学习进度
    public function progress()
    {
        return $this->hasOne(UserCourseTaskProgress::class)->withDefault([
            'status' => '0',
            'start_at' => null,
            'end_at' => null
        ]);
    }
}
